package com.whq.thrift.api.web.admin.controller.bs.test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.test.IceTest;
import com.whq.thrift.api.web.admin.service.test.IceTestService;
import com.whq.thrift.api.web.admin.util.LoginUtil;
import com.whq.thrift.api.web.admin.util.PageData;

@Controller
@Scope("prototype")
public class IceTestCaseController {

	private static final int DEFAULT_PAGE_SIZE = 25;

	@Autowired
	IceTestService iceTestService;

	@RequestMapping(value = "bs/icetest/save")
	public String save(
			@RequestParam(value = "ipAddress", required = false) String ipAddress, 
			@RequestParam(value = "port", required = false) String port,

			@RequestParam(value = "ctxParamName_1", required = false) String ctxParamName_1,
			@RequestParam(value = "ctxParamName_2", required = false) String ctxParamName_2,
			@RequestParam(value = "ctxParamName_3", required = false) String ctxParamName_3,

			@RequestParam(value = "ctxParamVals_1", required = false) String ctxParamVals_1,
			@RequestParam(value = "ctxParamVals_2", required = false) String ctxParamVals_2,
			@RequestParam(value = "ctxParamVals_3", required = false) String ctxParamVals_3,

			@RequestParam(value = "paramName_1", required = false) String paramName_1,
			@RequestParam(value = "paramName_2", required = false) String paramName_2,
			@RequestParam(value = "paramName_3", required = false) String paramName_3,
			@RequestParam(value = "paramName_4", required = false) String paramName_4,
			@RequestParam(value = "paramName_5", required = false) String paramName_5,
			@RequestParam(value = "paramName_6", required = false) String paramName_6,
			@RequestParam(value = "paramName_7", required = false) String paramName_7,
			@RequestParam(value = "paramName_8", required = false) String paramName_8,
			@RequestParam(value = "paramName_9", required = false) String paramName_9,
			@RequestParam(value = "paramName_10", required = false) String paramName_10,
			@RequestParam(value = "paramName_11", required = false) String paramName_11,
			@RequestParam(value = "paramName_12", required = false) String paramName_12,
			@RequestParam(value = "paramName_13", required = false) String paramName_13,
			@RequestParam(value = "paramName_14", required = false) String paramName_14,
			@RequestParam(value = "paramName_15", required = false) String paramName_15,
			@RequestParam(value = "paramName_16", required = false) String paramName_16,
			@RequestParam(value = "paramName_17", required = false) String paramName_17,
			@RequestParam(value = "paramName_18", required = false) String paramName_18,
			@RequestParam(value = "paramName_19", required = false) String paramName_19,
			@RequestParam(value = "paramName_20", required = false) String paramName_20,
			@RequestParam(value = "paramName_21", required = false) String paramName_21,
			@RequestParam(value = "paramName_22", required = false) String paramName_22,
			@RequestParam(value = "paramName_23", required = false) String paramName_23,
			@RequestParam(value = "paramName_24", required = false) String paramName_24,
			@RequestParam(value = "paramName_25", required = false) String paramName_25,

			@RequestParam(value = "paramVal_1", required = false) String paramVal_1, @RequestParam(value = "paramVal_2", required = false) String paramVal_2,
			@RequestParam(value = "paramVal_3", required = false) String paramVal_3, @RequestParam(value = "paramVal_4", required = false) String paramVal_4,
			@RequestParam(value = "paramVal_5", required = false) String paramVal_5, @RequestParam(value = "paramVal_6", required = false) String paramVal_6,
			@RequestParam(value = "paramVal_7", required = false) String paramVal_7, @RequestParam(value = "paramVal_8", required = false) String paramVal_8,
			@RequestParam(value = "paramVal_9", required = false) String paramVal_9, @RequestParam(value = "paramVal_10", required = false) String paramVal_10,
			@RequestParam(value = "paramVal_11", required = false) String paramVal_11,
			@RequestParam(value = "paramVal_12", required = false) String paramVal_12,
			@RequestParam(value = "paramVal_13", required = false) String paramVal_13,
			@RequestParam(value = "paramVal_14", required = false) String paramVal_14,
			@RequestParam(value = "paramVal_15", required = false) String paramVal_15,
			@RequestParam(value = "paramVal_16", required = false) String paramVal_16,
			@RequestParam(value = "paramVal_17", required = false) String paramVal_17,
			@RequestParam(value = "paramVal_18", required = false) String paramVal_18,
			@RequestParam(value = "paramVal_19", required = false) String paramVal_19,
			@RequestParam(value = "paramVal_20", required = false) String paramVal_20,
			@RequestParam(value = "paramVal_21", required = false) String paramVal_21,
			@RequestParam(value = "paramVal_22", required = false) String paramVal_22,
			@RequestParam(value = "paramVal_23", required = false) String paramVal_23,
			@RequestParam(value = "paramVal_24", required = false) String paramVal_24,
			@RequestParam(value = "paramVal_25", required = false) String paramVal_25,

			@RequestParam(value = "name", required = false) String name, Model model, HttpServletRequest request) {
		model.addAttribute("ipAddress", ipAddress);
		model.addAttribute("port", port);

		model.addAttribute("ctxParamName_1", ctxParamName_1);
		model.addAttribute("ctxParamName_2", ctxParamName_2);
		model.addAttribute("ctxParamName_3", ctxParamName_3);

		model.addAttribute("ctxParamVals_1", ctxParamVals_1);
		model.addAttribute("ctxParamVals_2", ctxParamVals_2);
		model.addAttribute("ctxParamVals_3", ctxParamVals_3);

		model.addAttribute("paramName_1", paramName_1);
		model.addAttribute("paramName_2", paramName_2);
		model.addAttribute("paramName_3", paramName_3);
		model.addAttribute("paramName_4", paramName_4);
		model.addAttribute("paramName_5", paramName_5);
		model.addAttribute("paramName_6", paramName_6);
		model.addAttribute("paramName_7", paramName_7);
		model.addAttribute("paramName_8", paramName_8);
		model.addAttribute("paramName_9", paramName_9);
		model.addAttribute("paramName_10", paramName_10);
		model.addAttribute("paramName_11", paramName_11);
		model.addAttribute("paramName_12", paramName_12);
		model.addAttribute("paramName_13", paramName_13);
		model.addAttribute("paramName_14", paramName_14);
		model.addAttribute("paramName_15", paramName_15);
		model.addAttribute("paramName_16", paramName_16);
		model.addAttribute("paramName_17", paramName_17);
		model.addAttribute("paramName_18", paramName_18);
		model.addAttribute("paramName_19", paramName_19);
		model.addAttribute("paramName_20", paramName_20);
		model.addAttribute("paramName_21", paramName_21);
		model.addAttribute("paramName_22", paramName_22);
		model.addAttribute("paramName_23", paramName_23);
		model.addAttribute("paramName_24", paramName_24);
		model.addAttribute("paramName_25", paramName_25);

		model.addAttribute("paramVal_1", paramVal_1);
		model.addAttribute("paramVal_2", paramVal_2);
		model.addAttribute("paramVal_3", paramVal_3);
		model.addAttribute("paramVal_4", paramVal_4);
		model.addAttribute("paramVal_5", paramVal_5);
		model.addAttribute("paramVal_6", paramVal_6);
		model.addAttribute("paramVal_7", paramVal_7);
		model.addAttribute("paramVal_8", paramVal_8);
		model.addAttribute("paramVal_9", paramVal_9);
		model.addAttribute("paramVal_10", paramVal_10);
		model.addAttribute("paramVal_11", paramVal_11);
		model.addAttribute("paramVal_12", paramVal_12);
		model.addAttribute("paramVal_13", paramVal_13);
		model.addAttribute("paramVal_14", paramVal_14);
		model.addAttribute("paramVal_15", paramVal_15);
		model.addAttribute("paramVal_16", paramVal_16);
		model.addAttribute("paramVal_17", paramVal_17);
		model.addAttribute("paramVal_18", paramVal_18);
		model.addAttribute("paramVal_19", paramVal_19);
		model.addAttribute("paramVal_20", paramVal_20);
		model.addAttribute("paramVal_21", paramVal_21);
		model.addAttribute("paramVal_22", paramVal_22);
		model.addAttribute("paramVal_23", paramVal_23);
		model.addAttribute("paramVal_24", paramVal_24);
		model.addAttribute("paramVal_25", paramVal_25);

		model.addAttribute("name", name);

		String iceResponse = "";
		if (StringUtil.isBlank(ipAddress) || StringUtil.isBlank(port) ) {
			model.addAttribute("iceResponse", iceResponse);
			return "bs/icetest/index";
		}

		Map<String, String> ctxMap = new HashMap<String, String>(0);
		ctxMap.put(ctxParamName_1, ctxParamVals_1);
		ctxMap.put(ctxParamName_2, ctxParamVals_2);
		ctxMap.put(ctxParamName_3, ctxParamVals_3);

		for (Iterator<Map.Entry<String, String>> iterator = ctxMap.entrySet().iterator(); iterator.hasNext();) {
			Map.Entry<String, String> entry = iterator.next();
			String paramName = entry.getKey();
			String paramVal = entry.getValue();
			if (StringUtil.isBlank(paramName) || StringUtil.isBlank(paramVal)) {
				iterator.remove();
			}
		}
		JSONObject ctxJson = new JSONObject();
		for (Iterator<Map.Entry<String, String>> iterator = ctxMap.entrySet().iterator(); iterator.hasNext();) {
			Map.Entry<String, String> entry = iterator.next();
			String paramName = entry.getKey();
			String paramVal = entry.getValue();
			if (StringUtil.isNotBlank(paramName) && StringUtil.isNotBlank(paramVal)) {
				ctxJson.put(paramName, paramVal);
			}
		}

		Map<String, String> paramMap = new HashMap<String, String>(0);
		paramMap.put(paramName_1, paramVal_1);
		paramMap.put(paramName_2, paramVal_2);
		paramMap.put(paramName_3, paramVal_3);
		paramMap.put(paramName_4, paramVal_4);
		paramMap.put(paramName_5, paramVal_5);
		paramMap.put(paramName_6, paramVal_6);
		paramMap.put(paramName_7, paramVal_7);
		paramMap.put(paramName_8, paramVal_8);
		paramMap.put(paramName_9, paramVal_9);
		paramMap.put(paramName_10, paramVal_10);
		paramMap.put(paramName_11, paramVal_11);
		paramMap.put(paramName_12, paramVal_12);
		paramMap.put(paramName_13, paramVal_13);
		paramMap.put(paramName_14, paramVal_14);
		paramMap.put(paramName_15, paramVal_15);
		paramMap.put(paramName_16, paramVal_16);
		paramMap.put(paramName_17, paramVal_17);
		paramMap.put(paramName_18, paramVal_18);
		paramMap.put(paramName_19, paramVal_19);
		paramMap.put(paramName_20, paramVal_20);
		paramMap.put(paramName_21, paramVal_21);
		paramMap.put(paramName_22, paramVal_22);
		paramMap.put(paramName_23, paramVal_23);
		paramMap.put(paramName_24, paramVal_24);
		paramMap.put(paramName_25, paramVal_25);

		for (Iterator<Map.Entry<String, String>> iterator = paramMap.entrySet().iterator(); iterator.hasNext();) {
			Map.Entry<String, String> entry = iterator.next();
			String paramName = entry.getKey();
			String paramVal = entry.getValue();
			if (StringUtil.isBlank(paramName) || StringUtil.isBlank(paramVal)) {
				iterator.remove();
			}
		}
		String method = null;
		JSONObject paramJson = new JSONObject();
		for (Iterator<Map.Entry<String, String>> iterator = paramMap.entrySet().iterator(); iterator.hasNext();) {
			Map.Entry<String, String> entry = iterator.next();
			String paramName = entry.getKey();
			String paramVal = entry.getValue();
			if (StringUtil.isNotBlank(paramName) && StringUtil.isNotBlank(paramVal)) {
				if ("method".toLowerCase().equals(paramName.trim())){
					method = paramVal;
				}
				paramJson.put(paramName, paramVal);
			}
		}

		IceTest iceTest = new IceTest();
		iceTest.setName(name);
		if (StringUtil.isNotBlank(method)){
			iceTest.setMethod(method);
		}
		iceTest.setIp(ipAddress);
		iceTest.setPort(port);
		iceTest.setCtxJson(ctxJson.toString());
		iceTest.setParamJson(paramJson.toString());
		String manager = LoginUtil.getLoginTicket(request);
		iceTest.setManager(StringUtil.isBlank(manager) ? "" : manager);
		int addicetestcase = iceTestService.addIceTest(iceTest);
		if (addicetestcase == 1) {
			iceResponse = "保存成功！";
		} else {
			iceResponse = "保存失败！";
		}
		model.addAttribute("iceResponse", iceResponse);
		return "bs/icetest/index";
	}

	@RequestMapping(value = "bs/icetestcase/list")
	public String list(
			@RequestParam(value = "name", required = false) String name, 
			@RequestParam(value = "method", required = false) String method,
			@RequestParam(value = "manager", required = false) String manager,
			@RequestParam(value = "currentPage", required = true, defaultValue = "1") Integer currentPage,
			@RequestParam(value = "pageSize", required = false) Integer pageSize, Model model) {
		PageData<IceTest> pageData = new PageData<IceTest>();
		pageData.setCurrentPage(currentPage);
		if ((pageSize == null) || (pageSize <= 0)) {
			pageSize = DEFAULT_PAGE_SIZE;
		}
		pageData.setPageSize(pageSize);

		// 查询参数
		Map<String, Object> whereParams = new HashMap<String, Object>(0);
		// 查询参数
		if (StringUtil.isNotBlank(name)) {
			whereParams.put("name", name);
		}
		if (StringUtil.isNotBlank(method)) {
			whereParams.put("method", method);
		}
		if (StringUtil.isNotBlank(manager)) {
			whereParams.put("manager", manager);
		}

		int totalCount = iceTestService.getIceTestCount(whereParams);
		pageData.setTotalCount(totalCount);

		int beginIndex = (currentPage - 1) * pageSize;
		List<IceTest> resultList = iceTestService.getIceTestByMap(whereParams, beginIndex, pageSize);

		pageData.setResultList(resultList);

		model.addAttribute("name", name);
		model.addAttribute("method", method);
		model.addAttribute("manager", manager);
		model.addAttribute("pageData", pageData);
		return "bs/test/icetestcase/list";
	}

	@RequestMapping(value = "bs/icetestcase/delicetestcase")
	public String delMethods(int[] delIds, Model model) {
		if (delIds != null) {
			for (int i = 0; i < delIds.length; i++) {
				this.iceTestService.delIceTest(delIds[i]);
			}
		}
		return "redirect:list.do";
	}
	
	@RequestMapping(value = "bs/icetestcase/show")
	public String show(@RequestParam(value = "id", required = true) Integer id, Model model) {

		IceTest icetestcase = this.iceTestService.getIceTestById(id);
		if (icetestcase == null) {
			model.addAttribute("errMsg", "缺少参数: 找不到方法!");
			return "error";
		}
		model.addAttribute("name", icetestcase.getName());
		model.addAttribute("ipAddress", icetestcase.getIp());
		model.addAttribute("port", icetestcase.getPort());
//		String method = icetestcase.getMethod();
//		String identity = null;
//		if (StringUtil.isNotBlank(method)){
//			List<String> methodList = StringUtil.split2list(method, "\\.");
//			if (methodList != null && methodList.size()==2){
//				identity = methodList.get(0);
//			}
//		}

		if (icetestcase.getCtxJson() != null) {
			JSONObject ctxJson = JSONObject.parseObject(icetestcase.getCtxJson());
			Iterator<String> iterator = ctxJson.keySet().iterator();
			int i = 1;
			while (iterator.hasNext()) {
				String key = iterator.next();
				String value = (String) ctxJson.get(key);
				if (StringUtil.isNotBlank(key) && StringUtil.isNotBlank(value)) {
					model.addAttribute("ctxParamName_" + i, key);
					model.addAttribute("ctxParamVals_" + i, value);
					i++;
				}
			}
		}
		if (icetestcase.getParamJson() != null) {
			JSONObject paramJson = JSONObject.parseObject(icetestcase.getParamJson());
			Iterator<String> iterator = paramJson.keySet().iterator();
			int i = 1;
			while (iterator.hasNext()) {
				String key = iterator.next();
				String value = (String) paramJson.get(key);
				if (StringUtil.isNotBlank(key) && StringUtil.isNotBlank(value)) {
					model.addAttribute("paramName_" + i, key);
					model.addAttribute("paramVal_" + i, value);
					i++;
				}
			}
		}

		return "bs/icetest/index";
	}
}
